package com.suoluo.zenadmin.common;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;

import org.springframework.util.StringUtils;

/**
 * 截取字符串工具类
 * @author jingruiguyue
 *
 */
public class StringUtil {

	
	public static String[] subStringIdAndName(String idAndName)
	{
		if(StringUtils.hasLength(idAndName))
		{
			//获取#号第一次在字符串中出现的位置，规范的参数是"12(酒店在本系统中的标识)#XX(酒店名称)"
			int first = idAndName.indexOf("#");
			String id = idAndName.substring(0, first);
			String name = idAndName.substring(first+1);
			String[] res = new String[2];
			res[0] = id;
			res[1] = name;
			return res;
		}
		else
		{
			return new String[0];
		}
		
	}
	
	/**
	 * 将编码格式为ISO-8859-1字符串转换为Utf-8格式
	 * @param changeStr
	 * @return
	 */
	public static String stringISO88591ChangeUTF8 (String changeStr)
	{
		try {
			String str = new String(changeStr.getBytes("ISO-8859-1"),"UTF-8");
			return str;
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException("字符串编码格式转换失败！");
		} 
	}
	
	
//	public static String filterMessyCode(String str){
//		StringBuilder buf = new StringBuilder(str.length());
//		for (char c : str.toCharArray()) {
//			if (c >= 0xF000 || c < 0xD000) {
//				buf.append(c);
//			}
//		}
//		return buf.toString();
//	}
	
	/**
	 * 过滤掉四个字节的字符
	 * @param text
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static String filterOffUtf8Mb4(String text) throws UnsupportedEncodingException {
		byte[] bytes = text.getBytes("utf-8");
		ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
		int i = 0;
		while (i < bytes.length) {
			short b = bytes[i];
			if (b > 0) {
				buffer.put(bytes[i++]);
				continue;
			}
			b += 256;
			if ((b ^ 0xC0) >> 4 == 0) {
				buffer.put(bytes, i, 2);
				i += 2;
			}
			else if ((b ^ 0xE0) >> 4 == 0) {
				buffer.put(bytes, i, 3);
				i += 3;
			}
			else if ((b ^ 0xF0) >> 4 == 0) {
				i += 4;
			}
			else{
				//如果不符合上面的要求，会直接陷入死循环，所以直接返回
				return text;
			}
		}
		buffer.flip();
		byte [] data = new byte[buffer.limit()];
//		System.out.println(buffer.limit());
//		System.out.println(buffer.position());
//		System.out.println(buffer.remaining());
//		System.out.println(buffer.capacity());
//		System.out.println(buffer.toString());
		buffer.get(data);
		return new String(data, "utf-8");
	}

	public static boolean isEmpty(String str){
		return !StringUtils.hasLength(str);
	}
	
	public static String buildCompleteName(String zhname,String enname){
		if (StringUtil.isEmpty(zhname)){
			return enname;
		} else if (zhname.equals(enname) || enname == null || enname.trim().length() == 0){
			return zhname;
		} else {
			return zhname +"（" + enname +"）";
		}
	}
	
	/**
	 * 截取字符串，
	 * @param source
	 * @param size
	 * @return
	 */
	public static String substring(String source,int size,String nullValue){
		if (!isEmpty(source) && source.length() > size){
			return source.substring(0, size);
		}else if (isEmpty(source) && !isEmpty(nullValue)){
			return nullValue;
		} else {
			return source;
		}
	}
	
	public static void main(String []args) throws UnsupportedEncodingException{
		String aa = "这是𠆤大遗憾了";
		System.out.println(aa);
		System.out.println(filterOffUtf8Mb4("这是ࠆ¤大遗憾了，"));
		System.out.println(filterOffUtf8Mb4(aa));
	}
}
